﻿using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// UGUI Button 类扩展
/// </summary>
public static class ButtonExtension
{

    public static void SetActive(this Button button, bool isActive)
    {
        button.gameObject.SetActive(isActive);
    }

    public static void SetGray(this Button btn, bool isGray)
    {
        // 修改至gameObject
        btn.gameObject.SetGray(isGray);
        // var imgs = btn.GetComponentsInChildren<Image>(true);
        // for (int i = 0, cnt = imgs.Length; i < cnt; i++)
        // {
        //     imgs[i].SetGray(isGray);
        // }
        //
        // var texts = btn.GetComponentsInChildren<Text>(true);
        // for (int i = 0, cnt = texts.Length; i < cnt; i++)
        // {
        //     texts[i].SetGray(isGray);
        // }
        //
        // var pss = btn.GetComponentsInChildren<ParticleSystem>(true);
        // for (int i = 0, cnt = pss.Length; i < cnt; i++)
        // {
        //     pss[i].gameObject.SetActive(!isGray);
        // }
    }

    /// <summary>
    /// 设置按钮上的文本，将会取得按钮之下第一个 Text 进行设置
    /// </summary>
    /// <param name="button"></param>
    /// <param name="content"></param>
    public static void SetText(this Button button, string content)
    {
        var text = button.GetComponentInChildren<Text>();
        if (text != null)
        {
            text.text = content;
        }
    }

    public static void SetSprite(this Button button, string spriteName, bool setNativeSize = false)
    {
        var image = button.GetComponent<Image>();
        if (image != null)
            image.SetSprite(spriteName, setNativeSize);
    }

    public static void SetOnClick(this Button button, UnityEngine.Events.UnityAction call)
    {
        button.onClick.RemoveAllListeners();
        button.onClick.AddListener(call);
    }
}
